package com.jkc.xh.views.widget.pullview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.jkc.xh.R;
import com.jkc.xh.views.widget.loadmore.LoadMoreContainer;
import com.jkc.xh.views.widget.loadmore.LoadMoreHandler;
import com.jkc.xh.views.widget.loadmore.LoadMoreListViewContainer;
import com.jkc.xh.views.widget.pullview.core.ListViewPlus;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by wangjie_91 on 2015/7/12.
 */
@EViewGroup(R.layout.view_pull_listview)
public class PullListView extends LinearLayout {
    public PullListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @ViewById(R.id.list_view)
    ListViewPlus mListView;
    @ViewById(R.id.list_view_ptr_frame)
    PtrFrameLayout mPtrFrameLayout;
    @ViewById(R.id.load_more_list_view_container)
    LoadMoreListViewContainer mLoadMoreListViewContainer;

    private OnPullListener mListener;

    @AfterViews
    void init() {
//        RefreshHeaderView header = new RefreshHeaderView(getContext());
//        mPtrFrameLayout.setHeaderView(header);
//        mPtrFrameLayout.addPtrUIHandler(header);
        mPtrFrameLayout.setLoadingMinTime(1000);
        mPtrFrameLayout.disableWhenHorizontalMove(true);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout ptrFrameLayout, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(ptrFrameLayout, mListView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                if (mListener != null) {
                    mListener.onRefresh(mListView);
                }
            }
        });

        mLoadMoreListViewContainer.useDefaultFooter();
        mLoadMoreListViewContainer.setAutoLoadMore(true);
        mLoadMoreListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                if (mListener != null) {
                    mListener.onLoadMore(mListView);
                }
            }
        });
    }

    public void finishLoad(boolean hasMore) {
        mPtrFrameLayout.refreshComplete();
        mLoadMoreListViewContainer.loadMoreFinish(hasMore);
    }

    public void setOnDetectScrollListener(ListViewPlus.OnDetectScrollListener onDetectScrollListener) {
        mListView.setOnDetectScrollListener(onDetectScrollListener);
    }

    public ListViewPlus getListView() {
        return mListView;
    }

    public PtrFrameLayout getRefreshView() {
        return mPtrFrameLayout;
    }

    public LoadMoreListViewContainer getLoadMoreView() {
        return mLoadMoreListViewContainer;
    }

    public void setRefreshHeader(View header) {
        mPtrFrameLayout.setHeaderView(header);
    }

    public void setOnPullListener(OnPullListener listener) {
        mListener = listener;
    }

    public void setPullToRefresh(boolean pullToRefresh) {
        mPtrFrameLayout.setPullToRefresh(pullToRefresh);
    }

    public void autoRefresh() {
        mPtrFrameLayout.autoRefresh();
    }

    public interface OnPullListener {
        void onRefresh(ListViewPlus listView);

        void onLoadMore(ListViewPlus listView);
    }


}
